/**
 * 
 */
package cn.antia.cep.page.action;

import java.util.*;

import javax.servlet.http.*;

import cn.antia.cep.common.*;
import cn.antia.cep.busi.*;
import cn.antia.cep.db.hibernate.bean.*;
import cn.antia.cwin.page.StrutsAction;

/**
 * @author sunkeei
 * 
 */
public class LoginAction extends StrutsAction {
	private String username;
	private String password;
	private String action;

	private LoginService loginService;
	public void setLoginService(LoginService loginService) {
		this.loginService = loginService;
	}
	
	public LoginAction(){}

	protected String action() throws AntiaException {
		if (username == null || username.equals("") || password == null
				|| password.equals("")) {
			request.setAttribute("err_msg", "用户名或密码为空，登录失败。");
			return "failure";
		}
		HashMap usermap = (HashMap) request.getSession().getServletContext().getAttribute(SessionConst.USER_MAP);
		VisitLog log = (VisitLog) usermap.get(session.getId());
		loginService = (LoginService)ContextBean.getBean("loginService");
		Privilege privilege = loginService.login(username, password, log);
		if(privilege != null){
			//存储权限信息到SESSION中
			session.setAttribute(SessionConst.PRIVILEGE, privilege);
			log.setPrivilege(privilege);
			return SUCCESS;
		}else{
			request.setAttribute("err_msg","登录失败，用户名或密码错误。");
			return "failure";
		}
	}

	public boolean checkSession(HttpSession session) {
		return true;
	}

	public void setPassword(String password) {
		this.password = password;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public void setAction(String action) {
		this.action = action;
	}
}